# Copyright (c) 2005-2010, Paolo Amoroso (Aioe) <freedom@aioe.org>
# All rights reserved.
# Postfilter (badwords.pm) version 0.8.2

use Date::Parse;
use Digest;
use strict;

our (%hdr, $modify_headers, $body, $user, %config, %settings, %postersdefault  );
our (%headlist, @quickref, @saved_headers, %whitelist, @distributions, %forbidden_crosspost, %scoreset, %maxscore);
our (@nomoderation, @htmlallowed, @htmltags, %extracontent, %forbidden_headers, @forbidden_groups, %dnsbl,@localip);


########
#
# badwords(): Badwords scanner
#
#######

sub badwords()
{
        my ( $total_body_score, $total_subject_score, $id ) = 0;
        my ($host, $time, $pid, $ip, $date, $client_dn, $length, $head_length, $md5, $domain, $gruppi, $followup) = &query_cache_header();


        &log( "debug", "Reading $config{'file_badwords'}" ) if ( $config{'verbose_log'} eq "true" );

#######################
# Read badwords.conf
#######################

	my $BADWORDS;

        unless( open( $BADWORDS, "$config{'file_badwords'}"))
        {
                &log( "err", "Unable to read data from $config{'file_badwords'}" );     
                return 38;
        }

        my @badwords = <$BADWORDS>;
        close $BADWORDS;

#######################
# Extract useful lines
#######################

        foreach (@badwords)
        {
                $id++;
                if ( $_ !~ /^\n|^\r|^\t|^\#|^\:|^\ |^[\ ][\t]+\n$/ )
                {
                        my ( $regex, $body_score, $subject_score, $comment ) = split( /\:/, $_, 3 );

#######################
# Update the counter if the current rule matches
#######################

                        if (
                                ( $regex ne "" ) and
                                (
                                        ( $body_score eq "0" ) or
                                        ( $body_score >   0  )
                                ) and
                                (
                                        ( $subject_score eq "0" ) or
                                        ( $subject_score >   0  )
                                )
                           )
                        {
                                $total_body_score += $body_score       if ( $body =~ /$regex/i );
                                $total_subject_score += $subject_score if ( $hdr{'Subject'} =~ /$regex/i );
                        }
                        else
                        {
                                &log( "err", "Syntax error in $config{'file_badwords'}, line $id" );
                                return 27;
                        }
                }
        }

#######################
# Check score on Subject
#######################

        if (
                ( $config{'scan_subject'} eq "true" ) and
                ( $total_subject_score > $config{'max_score_on_subject'} )
           )
        {
                &log( "err", "Badwords on Subject: score $total_subject_score, max is $config{'max_score_on_subject'}, rejected" );
                return 28;
        }

#######################
# Check score on body
#######################

        if (
                ( $config{'scan_body'} eq "true" ) and
                ( $total_body_score > $config{'max_score_on_body'} )
           )
        {
                &log( "err", "Badwords on Body: score $total_body_score, max is $config{'max_score_on_body'}, rejected" );
                return 29;
        }
}


1;
